package com.apes.fn.scm.used.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.plugin.soe.metadata.BusinessAttributeValue;
import com.apes.framework.util.DateUtil;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import com.apes.scm.masterdata.stock.model.Location;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 功能：旧件入库(无赠品)
 *
 * @author BieBi
 * @create 2018-9-4 09:52:00
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = "war_used_store")
public class UsedStore extends Model {
    public static final String CONST_TABLE_NAME = "war_used_store";

    @Id
    @Column(name = "id", length = 16)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "旧件入库",prefix = "CR%(company)%(yy)%(month)", padding = 5, useDateRange=true)
    private String id;  //入库单号

    @Column(name = "source", length = 32)
    @Comment("来源单号")
    private String source;

    @Column(name = "source_business_type", length = 32)
    @Comment("来源单据类型")
    private String sourceBusinessType;

    @Column(name = "source_business_type_name", length = 64)
    @Comment("来源单据类型名称")
    private String sourceBusinessTypeName;

    @Transient
    private String sourceTypeAndName;

    public String getSourceTypeAndName() {
        if (source != null&& sourceBusinessTypeName !=null) return "["+source+"]"+sourceBusinessTypeName;
        return null;
    }

    @Comment("公司")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    private PartyRole company; //公司编码

    @Comment("门店")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_id", referencedColumnName = "id", nullable = false)
    private Dept dept;

    @Comment("仓库")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "location_id", referencedColumnName = "id", nullable = false)
    private Location location;

    @Comment("保险回收标记")
    @Column(name = "insurance_recovery_sign", nullable = false, columnDefinition = "number(1,0) default 0")
    private boolean insuranceRecoverySign = false; //保险回收标记

    @Comment("入库类型")
    @States(name = "入库类型", values = {
            @State(id = "normal", name = "正常入库"),
            @State(id = "inventoryProfit", name = "盘盈入库")})
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "store_type", referencedColumnName = "id", nullable = false)
    private BusinessAttributeValue storeType;


    @States(name = "订单状态", values = {
            @State(id = "create", name = "创建"),
            @State(id = "done", name = "验收"),
            @State(id = "delete", name = "删除"),
    })
    @Comment("状态")
    @Column(name = "state", length = 10, nullable = false)
    private String state;

    @Comment("备注")
    @Column(name = "remark", length = 256)
    private String remark;  //备注

    @Comment("删除日期")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "delete_id", referencedColumnName = "id")
    private PartyRole deleteId; //删除人

    @Comment("删除日期")
    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "delete_date")
    private Date deleteDate;    // 删除日期

    @Comment("验收人")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "check_id", referencedColumnName = "id")
    private PartyRole checkId; // 验收人

    @Comment("验收日期")
    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "check_date")
    private Date checkDate;    // 验收日期


    /***
     * 功能： 旧件入库单明细
     */
    @OneToMany(mappedBy = "usedStore", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<UsedStoreItem> usedStoreItems = new ArrayList();

    public List<UsedStoreItem> getUsedStoreItems() {
        if (usedStoreItems == null) return null;
        for (UsedStoreItem item : usedStoreItems) {
            item.setUsedStore(this);
            item.setCompany(this.company);
        }
        return usedStoreItems;
    }


//    @ManyToOne(fetch = FetchType.LAZY)
//    @JoinColumn(name = "vip_id", referencedColumnName = "id", nullable = false)
//    private Party vip;  // 会员
//
//    @Column(name = "mobile_number", length = 64)
//    private String mobileNumber; //手机号码
//
//
//    @ManyToOne(fetch = FetchType.LAZY)
//    @JoinColumn(name = "car_base_id", referencedColumnName = "id")
//    private CarBase carBase;     // 车辆唯一标示符
//
//    @Column(name = "plate_number", length = 16)
//    private String plateNumber; //车牌号码
//
//    @ManyToOne(fetch = FetchType.LAZY)
//    @JoinColumn(name = "car_label_id", referencedColumnName = "id")
//    private CarLabel carLabel;//厂牌
//
//
//    @ManyToOne(fetch = FetchType.LAZY)
//    @JoinColumn(name = "car_model_id", referencedColumnName = "id")
//    private CarModel carModel;  //车型
//
//    @ManyToOne(fetch = FetchType.LAZY)
//    @JoinColumn(name = "car_configuration_id", referencedColumnName = "id")
//    private CarConfiguration carConfiguration;     //车型配置
//
//    @Column(name = "income_id", length = 500)
//    private String income_id; // 资金收入单号
//
//    @Column(name = "insurance_recovery_sign", nullable = false, columnDefinition = "number(1,0) default 0")
//    private boolean insuranceRecoverySign = false; //保险回收标记

    @Transient
    private PartyRole _creator;

    @Transient
    public PartyRole get_creator() {
        if(this.getCreator() == null) return null;
        return SpringManager.getBean(PartyRoleRepository.class).findOne(this.getCreator());
    }

}
